package com.colorlight.cloud.common.utils;

import java.math.BigInteger;

/**
 * @author Demon
 * @date 9/4/2024  上午10:18
 */
public class HexUtil {
	
	/**
	 * @param array 10进制数组
	 * @return 16进制字符串
	 * 10进制数组转16进制字符串
	 **/
	public static String toHex(byte[] array) {
		BigInteger bi = new BigInteger(1, array);
		String hex = bi.toString(16);
		int paddingLength = (array.length * 2) - hex.length();
		if (paddingLength > 0) {
			return String.format("%0" + paddingLength + "d", 0) + hex;
		} else {
			return hex;
		}
	}
	
	
	/**
	 * @param hex 16进制字符串
	 * @return 10进制数组
	 * @author Demon
	 * @date 2022/4/2/15:31
	 * 十六进制字符串转10禁止数组
	 **/
	public static byte[] fromHex(String hex) {
		byte[] binary = new byte[hex.length() / 2];
		for (int i = 0; i < binary.length; i++) {
			binary[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
		}
		return binary;
	}
}
